<%--
  Created by IntelliJ IDEA.
  User: Zhangxq
  Date: 2016/7/16
  Time: 0:07
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%--<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>--%>
<%--<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>--%>
<html>
<head>
    <title>用户信息列表导出</title>
    <%--<script type="text/javascript" src="http://localhost:9080/js/jquery-3.1.0.min.js">
    </script>--%>
    <script>
        //方法1，将前端的数据以json字符串的格式导入到后端
        function outExcel(){
            /* var formList=[];
             var items = document.getElementsByName("checkboxOne");
             var isNull=true;
             for (var i = 0; i < items.length; ++i)
             {
             if (items[i].checked)
             {
             var tr=items[i].parentNode.parentNode;
             var $tr=$(tr);
             formList.push(
             {
             "id": $tr.find("td").eq(1).text(),
             "userName": $tr.find("td").eq(2).text(),
             "userPhone": $tr.find("td").eq(3).text(),
             "userEmail": $tr.find("td").eq(4).text()
             }
             )
             isNull=false;
             }
             }

             if(isNull==false){
             var last=JSON.stringify(formList); //将JSON对象转化为JSON字符
             var form=$("<form>");
             form.attr('style','display:none');
             form.attr('target','');
             form.attr('method','post');
             form.attr('action','http://localhost:9080/excel/outExcel.do');
             //添加input
             var input1 = $("<input>");
             input1.attr('type','hidden');
             input1.attr('name','userList');
             input1.attr('value',last);
             $('body').append(form);
             form.append(input1);
             form.submit();//提交表单
             }else{
             alert("您未选择导出的数据！");
             }
             */

        }

        function outExcel2(){
            /* var formList=[];
             var items = document.getElementsByName("checkboxOne");
             var isNull=true;
             for (var i = 0; i < items.length; ++i)
             {
             if (items[i].checked)
             {

             formList.push(
             items[i].value
             )
             isNull=false;
             }
             }

             if(isNull==false){
             var last=JSON.stringify(formList); //将JSON对象转化为JSON字符
             var form=$("<form>");
             form.attr('style','display:none');
             form.attr('target','');
             form.attr('method','post');
             form.attr('action','http://localhost:9080/excel/outExcel2.do');
             //添加input
             var input1 = $("<input>");
             input1.attr('type','hidden');
             input1.attr('name','userList');
             input1.attr('value',last);
             $('body').append(form);
             form.append(input1);
             form.submit();//提交表单
             }else{
             alert("您未选择导出的数据！");
             }*/

        }
        function singleSelected(obj){
            /* if(obj.checked){
             var items=document.getElementsByName("checkboxOne");
             var isAll=true;
             for(var i=0;i<items.length;i++){
             if(items[i].checked==false){
             isAll=false;
             return;
             }
             }
             if(isAll){
             document.getElementsByName("checkboxAll")[0].checked=true;
             }
             }else{
             document.getElementsByName("checkboxAll")[0].checked=false;
             }*/
        }

        function allSelected(checkboxAll,checkboxOne){
            /*   var all_item = document.getElementsByName(checkboxAll);
             var items = document.getElementsByName(checkboxOne);
             for (var i = 0; i < items.length; ++i)
             {
             if (all_item[0].checked)
             {
             items[i].checked = true;
             }
             else
             {
             items[i].checked = false;
             }
             }*/
        }

        function getPlace(){
            ajaxYS("http://localhost:8080/place/insertAll");
        }
        function ajaxYS(url){
            var myXMLHttpRequest = null;
            if (window.ActiveXObject) {
                myXMLHttpRequest = new ActiveXObject("Microsoft.XMLHTTP");
            } else {
                myXMLHttpRequest = new XMLHttpRequest();
            }
            // var url = "http://localhost:8080/pinyin/getHead";
            var sourceurl=document.getElementById("sourceurl").value;
             var postData = {
                "sourceurl": sourceurl
            }
            myXMLHttpRequest.open("post", url, true);
            myXMLHttpRequest.setRequestHeader("Content-Type","application/json");
            //myXMLHttpRequest.send(postData);
            myXMLHttpRequest.send(JSON.stringify(postData));
            myXMLHttpRequest.onreadystatechange = function(){
                if (myXMLHttpRequest.readyState == 4) {
                    if(myXMLHttpRequest.status == 200){
                        alert("ok");
                    }
                }
            }
        }
    </script>
</head>

<body>
<table>
    <tr>
        <td>全选<input type="checkbox" name="checkboxAll" value="${user.id}" onclick="allSelected('checkboxAll','checkboxOne')"/></td>
        <th>用户序号</th>
        <th>姓名</th>
        <th>手机号</th>
        <th>邮箱</th>
    </tr>
    <tbody id="trs">
    <c:if test="${!empty userList}">
        <c:forEach var="user" items="${userList}">
            <tr>
                <td><input type="checkbox" name="checkboxOne" value="${user.id}" onclick="singleSelected(this)"/></td>
                <td>${user.id}</td>
                <td>${user.userName}</td>
                <td>${user.userPhone}</td>
                <td>${user.userEmail}</td>
            </tr>
        </c:forEach>
    </c:if>
    </tbody>
</table>
<hr>
<button onclick="outExcel();">导出Excel</button>
<hr>
<button onclick="outExcel2();">导出Excel第二套方案</button>
<hr>
<input type="text" id="sourceurl" value="http://www.stats.gov.cn/tjsj/tjbz/tjyqhdmhcxhfdm/2015">
<br>
<button onclick="getPlace()">得到所有省市区</button>

</body>
</html>